package com.catalog.CustomComponent
{
	import Skin.Accordion.CustomAccordionContentSkin;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Text;
	
	import spark.components.Group;
	import spark.components.Label;
	import spark.components.SkinnableContainer;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.core.IDisplayText;
	import spark.layouts.VerticalLayout;
	
	[Event(name="openChanged",type="flash.events.Event")]
	
	[SkinState("normal")]
	
	[SkinState("open")]
	
	[SkinState("disabled")]
	
	public class CustomAccordionContent extends SkinnableContainer
	{
//		[SkinPart(required="true")]
//		public var iconImage:SkinnableComponent;
		
		[SkinPart(required="true")]
		public var labelDisplay:IDisplayText;
		
		[SkinPart(required="true")]
		public var barGroup:Group;
		
		public function CustomAccordionContent()
		{
			super();
			setStyle("skinClass",CustomAccordionContentSkin);
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			contentGroup.clipAndEnableScrolling = true;
		}
		
		
		private var _title:String;

		[Bindable]
		[Inspectable(category="General", defaultValue="")]
		public function get title():String
		{
			return _title;
		}

		public function set title(value:String):void
		{
			_title = value;
			if( labelDisplay )
				labelDisplay.text = _title;
		}
		
		private var _isOpen:Boolean = false;
		
		public function get isOpen():Boolean
		{
			return _isOpen;
		}
		
		public function set isOpen(value:Boolean):void
		{
			if( _isOpen != value )
			{
				_isOpen = value;
				invalidateSkinState();
				invalidateDisplayList();
			}
		}
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded( partName,instance );
			if( partName == "labelDisplay" )
				labelDisplay.text = title;
			if( partName == "barGroup" )
				barGroup.addEventListener(MouseEvent.CLICK,onBarClickHandler );
		}
		
		override protected function partRemoved(partName:String, instance:Object):void
		{
			if( partName == "barGroup" )
				barGroup.removeEventListener(MouseEvent.CLICK,onBarClickHandler );
		}
		
		protected function onBarClickHandler( event:Event ):void
		{
			isOpen = !isOpen;
			dispatchEvent( new Event("openChanged"));
		}
		
		override protected function getCurrentSkinState():String
		{
			var returnState:String = "normal";
			if( !enabled )
				returnState = "disabled";
			else if( isOpen )
				returnState = "open";
			return returnState;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			var barWidth:Number = unscaledWidth;
			var barHeight:Number = barGroup.height;
			barGroup.setLayoutBoundsSize(barWidth,barHeight);
			barGroup.setLayoutBoundsPosition(0,0);
			if( isOpen )
			{
				contentGroup.width = unscaledWidth;
				contentGroup.height = contentGroup.contentHeight;
//				contentGroup.setLayoutBoundsSize(unscaledWidth,contentGroup.contentHeight);
			}
			else
			{
				contentGroup.width = unscaledWidth;
				contentGroup.height = 0;
//				contentGroup.setLayoutBoundsSize(unscaledWidth,0);
			}
		}
		
		public function getContentHeight():Number
		{
			return contentGroup.contentHeight+barGroup.height;
		}
	}
}